<template>
  <div class="roles">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容..." clearable="">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="success" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" ></el-table-column>
        <el-table-column prop="goods_price" label="价格(元)" width="80"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="100"></el-table-column>
        <el-table-column  label="操作" width="120">
          <!-- 编辑 -->
          <el-button type="primary" icon="el-icon-edit" size="small"></el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="small"></el-button>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 30,40]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表数据
      goodsList:[],
      // 请求商品参数
      queryGoodsInfo:{
        query:'',
        pagenum:1, // 当前页码
        pagesize:10 // 每页显示条数
      },
      // 商品总数
      total:0,
      currentPage:1
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const {data:res} = await this.$http.get('goods',{ params:this.queryGoodsInfo} )
      console.log(res);
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.total = res.data.total;
      this.goodsList = res.data.goods;
    },
    // 添加商品
    addGoods() {
      this.$router.push('/goods/add');
    }
  },
};
</script>

<style scoped>
.el-row {
  margin-bottom: 15px;
}
</style>